﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Text;

//自定义分页控件
namespace IService.InfoControls
{
    [ToolboxData("<{0}:PagingControl runat=\"server\"></{0}:PagingControl>"), DefaultProperty("")]
    partial class PagingControl : Literal
    {
        #region 分页属性
        private int pageSize = 10;
        public int PageSize
        {
            set
            {
                if (value > 0)
                {
                    pageSize = value;
                }
            }
            get { return pageSize; }
        }        
        private int pageIndex = 1;
        public int PageIndex
        {
            set
            {
                if (value > 0) { pageIndex = value; }
            }
            get { return pageIndex; }
        }
        public int RecordCount { set; get; }
        private int PageCount = 0;
        //分页参数        
        private string paraName = "page";
        public string ParaName
        {
            set
            {
                if (value != null && value.Trim() != "")
                {
                    paraName = value.Trim();
                }
            }
            get
            {
                return paraName;
            }
        }
        #endregion
        #region Css
        //分页控件的样式        
        public string CssClass { set; get; }
        //上一页样式        
        public string CssPrev { set; get; }
        //下一页样式        
        public string CssNext { set; get; }
        //当前页样式        
        public string CssCurrent { set; get; }
        //首页样式       
        public string CssFirst { set; get; }
        //尾页样式       
        public string CssLast { set; get; }
        //其它页样式       
        public string CssOther { set; get; }
        #endregion
        #region Img
        //首页图片        
        public string ImgFirst { set; get; }
        //上一页页图片        
        public string ImgPrev { set; get; }
        //下一页页图片       
        public string ImgNext { set; get; }
        //尾页        
        public string ImgLast { set; get; }
        #endregion
        // A标签的HRef        
        private string ATargetHref { set; get; }
        //获取paraname之外的请求参数        
        private void GetUrl()
        {
            HttpContext ctx = HttpContext.Current;
            string hostName = "http://" + ctx.Request.Url.Host + ":" + ctx.Request.Url.Port;
            string url = ctx.Request.Url.ToString().Substring(hostName.Length).Split('?')[0] + "?";
            StringBuilder query = new StringBuilder(url);
            foreach (string key in ctx.Request.QueryString)
            {
                if (key != null && key.ToLower() != this.ParaName)
                {
                    query.Append(string.Format("{0}={1}&", key, ctx.Request.QueryString[key]));
                }
            }
            this.ATargetHref = query.ToString();
        }
        private string GetUrl(int pageIndex)
        {
            return string.Format("{0}{1}={2}", this.ATargetHref, this.paraName, pageIndex);
        }
        //初始化属性        
        private void Initializtion()
        {
            GetUrl();
            this.PageCount = (this.RecordCount - 1) / this.PageSize + 1;
            if (!string.IsNullOrEmpty(HttpContext.Current.Request[this.ParaName]))
            {
                this.PageIndex = int.Parse(HttpContext.Current.Request[this.ParaName]);
            }
        }
        //生成控件        
        private void LoadControl(List<Tag> divPaging)
        {
            if (divPaging == null || divPaging.Count == 0)
            {
                this.Text = null;
                return;
            } StringBuilder sb = new StringBuilder(256);
            if (this.CssClass == null || this.CssClass.Trim() == "")
            {
                sb.Append("<div>");
            }
            else
            {
                sb.Append(string.Format("<div class='{0}'>", this.CssClass));
            }
            for (int i = 0; i < divPaging.Count; i++)
            {
                if (divPaging[i] == null)
                { sb.Append(GetCurrentPage()); }
                else
                { sb.Append(divPaging[i].ToString()); }
            }
            sb.Append("</div>"); this.Text = sb.ToString();
        }
        private string GetCurrentPage()
        {
            if (this.CssCurrent == null || this.CssCurrent.Trim() == "")
            {
                return string.Format("<span>{0}</span>", this.PageIndex);
            }
            else
            {
                return string.Format("<span class={0}>{1}</span>", this.CssCurrent, this.PageIndex);
            }
        }
        /// <summary>        
        /// 加载控件        
        /// </summary>        
        public void LoadControl()
        {
            //如果分页数据小于每页数据则不显示分页标签
            //if (this.RecordCount <= this.PageSize) { this.Text = null; }
            //else
            //{
                Initializtion();
                LoadControl(AddATarget());
            //}
        }
        #region 创建标签
        private Tag CreateATarget(string img, string css, int pageIndex, string content)
        {
            Tag a = new Tag(); a.HRef = GetUrl(pageIndex);
            if (img == null || img.Trim() == "")
            {
                a.InnerHtml = content;
            }
            else
            {
                a.InnerHtml = string.Format("<img src={0}/>", img);
            }
            if (css != null && css.Trim() != "") { a.CssClass = css; }
            return a;
        }
        #endregion
        // 向集合中添加标签        
        private List<Tag> AddATarget()
        {
            List<Tag> divPaging = new List<Tag>();
            if (this.PageIndex > 1)
            //添加上一页            
            {
                divPaging.Add(CreateATarget(this.ImgFirst, this.CssFirst, 1, "首页"));
                //添加首页                
                divPaging.Add(CreateATarget(this.ImgPrev, this.CssPrev, this.PageIndex - 1, "上一页"));
            } int end = this.PageIndex + 5; if (end < 11)
            { end = 11; } if (end > this.PageCount)
            {
                end = this.PageCount;
            }
            int start = end - 10 > 0 ? end - 10 : 1;
            for (int i = start; i <= end; i++)
            {
                if (i == this.PageIndex)
                {
                    divPaging.Add(null);
                }
                else
                {
                    divPaging.Add(CreateATarget(null, this.CssOther, i, i.ToString()));
                }
            } if (this.PageIndex < this.PageCount)
            {
                divPaging.Add(CreateATarget(this.ImgNext, this.CssNext, this.PageIndex + 1, "下一页"));
                //添加下一页                
                divPaging.Add(CreateATarget(this.ImgLast, this.CssLast, this.PageCount, "尾页"));
                //添加尾页            
            }
            return divPaging;
        }
        // 标签实体        
        private class Tag
        {
            //链接            
            public string HRef { set; get; }
            //链接显示的内容            
            public string InnerHtml { set; get; }
            //样式           
            public string CssClass { set; get; }
            public override string ToString()
            {
                if (this.CssClass != null && this.CssClass.Trim() != "")
                {
                    this.CssClass = string.Format(" class='{0}'", this.CssClass);
                }
                return string.Format("<a href='{0}'{1}>{2}</a>", this.HRef, this.CssClass, this.InnerHtml);
            }
        }
    }
}